package cn.hiapi.user.api;

import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shop.vo.ShopBase;
import cn.hiapi.shopkeeper.entity.order.MyActivityOrder;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.order.MyActivityOrderService;
import cn.hiapi.user.query.MyActivityOrderQuery;
import cn.hiapi.user.vo.MyActivityOrderVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/1/2 20:58
 */
@RestController
@RequestMapping("/user/my-service")
public class OrderMyActivityController extends BasicQueryController<MyActivityOrder, Long, MyActivityOrderVo, MyActivityOrderQuery> {

    protected OrderMyActivityController(MyActivityOrderService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }


    @Override
    public List<MyActivityOrderVo> toListVo(List<MyActivityOrder> list, String[] fields) {
        return list.stream().filter(e->e.getActivityType()!= ActivityType.CARD).map(e -> this.toData(e, fields)).collect(Collectors.toList());
    }


    @Override
    public MyActivityOrderQuery setQuery(MyActivityOrderQuery query) {
        Token<Object> token = TokenGet.get();
        query.setBuyerId(token.getFid());
        return super.setQuery(query);
    }

    @Override
    public MyActivityOrderVo toData(MyActivityOrder data, String[] fields) {
        MyActivityOrderVo vo = data.copy(new MyActivityOrderVo());
        Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(data.getShopId());
        if (shop != null) vo.setShop(shop.copy(new ShopBase()));

        BasicEntity entity = this.dispatchContext.getServiceOne(ActivityFactory.class).get(vo.getActivityType()).getByAid(vo.getAid());
        vo.setExpand(entity);
        return vo;
    }
}
